<template>
  <div>
    <a-button type="primary" @click="showConfirm">{{ text }}</a-button>
  </div>
</template>
<script>
import axios from "axios";

export default {
  props: {
    text: { type: [String] },
    displayType: { type: [String], default: 'BUTTON' },
    callback: { type: [Function] },
    rows: { type: [Array], default: () => { return {} } },
    url: { type: [String] },
    buildingName: { type: [String] },
  },
  data() {
    return {
        form: this.$form.createForm(this, {name: this._uid}),
        visible: false,
        data: [],
        loading: false,
        selectedRowKeys: [],
        selectedRows: [],
    }
  },
  methods: {
    showConfirm() {
      if (this.rows.length == 0) {
        this.$error({
          title: this.$t('error_message'),
          content: this.$t('please_choose_one_record'),
          okText: this.$t('modal_ok'),
        })
        return
      }
      let _this = this
      this.$confirm({
        title: this.$t('confirm_message'),
        content: this.$t('please_confirm_submit_selected_records'),
        okText: this.$t('confirm_yes'),
        okType: 'danger',
        cancelText: this.$t('confirm_no'),
        onOk() {
          _this.handleOk()
        },
      })
    },
    handleOk() {
      let selectedIds = this.rows.map(item => item.id)
      axios({
        method: 'post',
        url: this.url,
        data: {
          analysisIdList:selectedIds,
          buildingName:this.buildingName
        },
      }).then(() => {
        if(this.callback) {
          this.callback()
        }
        this.$success({
          title: this.$t('success_message'),
          content: this.$t('submit_success'),
          okText: this.$t('modal_ok'),
        });
      })
    },
  },
  watch:{
    buildingName(val) {
      this.buildingName = val;
    }
  }
}
</script>
<style>
.control-set{
    margin-top:8px ;
}
</style>